package com.pgytesting.secondhandcar.service.impl;

import com.pgytesting.secondhandcar.dao.*;
import com.pgytesting.secondhandcar.model.*;
import com.pgytesting.secondhandcar.service.SupplementaryBasicInformationService;
import com.pgytesting.secondhandcar.util.SessionUtil;
import com.pgytesting.secondhandcar.util.UtilMethod;
import com.pgytesting.secondhandcar.vo.Parameters;
import com.pgytesting.secondhandcar.vo.ResponseMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 补充基本信息(SupplementaryBasicInformation)表服务实现类
 *
 * @author makejava
 * @since 2019-08-12 18:55:58
 */
@Slf4j
@Service("supplementaryBasicInformationService")
public class SupplementaryBasicInformationServiceImpl implements SupplementaryBasicInformationService {
    @Resource
    private SupplementaryBasicInformationDao supplementaryBasicInformationDao;

    @Resource
    private OrderNameDao orderNameDao;

    @Resource
    private McpUserInfoDao mcpUserInfoDao;

    @Resource
    private WorkInfoDao workInfoDao;

    @Resource
    private RelativesAndFriendsDao relativesAndFriendsDao;

    @Resource
    private LoanInformationDao loanInformationDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SupplementaryBasicInformation queryById(String id) {
        return this.supplementaryBasicInformationDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<SupplementaryBasicInformation> queryAllByLimit(int offset, int limit) {
        return this.supplementaryBasicInformationDao.queryAllByLimit(offset, limit);
    }

    /**
     * 条件查询多条数据
     *
     * @param supplementaryBasicInformation 实例对象
     * @return 对象列表
     */
    @Override
    public List<SupplementaryBasicInformation> queryAll(SupplementaryBasicInformation supplementaryBasicInformation) {
        return this.supplementaryBasicInformationDao.queryAll(supplementaryBasicInformation);
    }

    /**
     * 新增数据
     *
     * @param supplementaryBasicInformation 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public ResponseMessage insert(SupplementaryBasicInformation supplementaryBasicInformation) throws Exception {
        // 获取到当前的订单ID
        String orderNumber = supplementaryBasicInformation.getId();
        // 获取订单信息，查询用户账号
        OrderName orderName = orderNameDao.queryById(orderNumber);
        if (!SessionUtil.getSessionMcpUser().getUserNumber().equals(orderName.getUserNumber())) {
            return ResponseMessage.errorResponse("用户信息验证失败，非法请求");
        }

        // 查询用户信息
        McpUserInfo mcpUserInfo = mcpUserInfoDao.queryById(orderName.getUserNumber());
        if (ObjectUtils.isEmpty(mcpUserInfo)) {
            return ResponseMessage.errorResponse("查询用户信息失败");
        }
        // 将用户信息插入到信息中
        supplementaryBasicInformation.setUserNumber(mcpUserInfo.getUserNumber());
        // 验证驾驶证型号是否正确
        if (!UtilMethod.checkDriversLicenseModel(supplementaryBasicInformation.getDriversLicenseModel())) {
            return ResponseMessage.errorResponse("驾驶证型号有误");
        }
        // 验证学历信息
        if (!UtilMethod.checkEducation(supplementaryBasicInformation.getEducation())) {
            return ResponseMessage.errorResponse("学历信息验证失败");
        }
        // 准备工作性质数据，准备入库
        WorkInfo workInfo = supplementaryBasicInformation.getWorkInfo();
        workInfo.setCreatedBy(mcpUserInfo.getPhoneNumber());
        workInfo.setId(UtilMethod.getUniqueCode());
        workInfo.setCreatedBy(mcpUserInfo.getPhoneNumber());
        // 工作性质ID更新到补充信息对象
        supplementaryBasicInformation.setProfessionalNatureId(workInfo.getId());
        // 获取亲属朋友信息
        RelativesAndFriends andFriends = supplementaryBasicInformation.getRelativesAndFriends();
        andFriends.setId(UtilMethod.getUniqueCode());
        andFriends.setCreatedBy(mcpUserInfo.getPhoneNumber());
        andFriends.setCreatedBy(mcpUserInfo.getPhoneNumber());
        if (!UtilMethod.checkRelationship(andFriends.getRelationship())) {
            return ResponseMessage.errorResponse("联系人关系验证错误");
        }
        // 工作性质ID更新到补充信息对象
        supplementaryBasicInformation.setImmediateFamilyId(andFriends.getId());

        // 获取工作证明人
        RelativesAndFriends workProver = supplementaryBasicInformation.getWorkProver();
        workProver.setId(UtilMethod.getUniqueCode());
        workProver.setCreatedBy(mcpUserInfo.getPhoneNumber());
        workProver.setCreatedBy(mcpUserInfo.getPhoneNumber());
        if (!UtilMethod.checkRelationship(workProver.getRelationship())) {
            return ResponseMessage.errorResponse("联系人关系验证错误");
        }
        //工作证明人ID
        supplementaryBasicInformation.setWorkProverId(workProver.getId());
        // 获取贷款信息
        LoanInformation loanInformation = supplementaryBasicInformation.getLoanInformation();
        loanInformation.setId(UtilMethod.getUniqueCode());
        loanInformation.setCreatedBy(mcpUserInfo.getPhoneNumber());
        loanInformation.setUserNumber(mcpUserInfo.getUserNumber());
        //贷款信息ID
        supplementaryBasicInformation.setLoanInformationId(loanInformation.getId());
        supplementaryBasicInformation.setCreatedBy(mcpUserInfo.getPhoneNumber());
        // 填入id以及用户id
        supplementaryBasicInformation.setId(UtilMethod.getUniqueCode());
        supplementaryBasicInformation.setUserNumber(mcpUserInfo.getUserNumber());
        log.info("数据准备完成，进行工作性质数据入库操作");
        workInfoDao.insert(workInfo);
        relativesAndFriendsDao.insert(andFriends);
        log.info("亲属朋友信息入库完成，进行工作证明人入库");
        relativesAndFriendsDao.insert(workProver);
        log.info("工作证明人信息入库完成，进行贷款信息入库");
        loanInformationDao.insert(loanInformation);
        log.info("贷款信息入库完成，进行用户补充信息入库");
        supplementaryBasicInformationDao.insert(supplementaryBasicInformation);
        log.info("修改订单状态");
        OrderName orderName1 = new OrderName();
        orderName1.setUpdatedBy(mcpUserInfo.getPhoneNumber());
        orderName1.setId(orderNumber);
        orderName1.setOrderStatus(Parameters.ORDER_21002);
        orderName1.setSupplementaryBasicInformationId(supplementaryBasicInformation.getId());
        orderNameDao.update(orderName1);
        return ResponseMessage.successResponse("用户补充信息插入成功");
    }

    /**
     * 修改数据
     *
     * @param supplementaryBasicInformation 实例对象
     * @return 实例对象
     */
    @Override
    public SupplementaryBasicInformation update(SupplementaryBasicInformation supplementaryBasicInformation) {
        this.supplementaryBasicInformationDao.update(supplementaryBasicInformation);
        return this.queryById(supplementaryBasicInformation.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.supplementaryBasicInformationDao.deleteById(id) > 0;
    }

    @Override
    public Map<String, Object> querySupplementaryInformation(SupplementaryBasicInformation supplementaryBasicInformation) {
    	Map<String, Object> map = new HashMap<String, Object>();
    	Map<String, Object> sbMap = supplementaryBasicInformationDao.querySupplementaryInformation(supplementaryBasicInformation);
    	map.put("supplementaryInformation", sbMap);
    	List<String> idList = new ArrayList<String>();
    	if(sbMap.get("IMMEDIATE_FAMILY_ID") != null)
    		idList.add(sbMap.get("IMMEDIATE_FAMILY_ID").toString());
    	if(sbMap.get("WORK_PROVER_ID") != null)
    		idList.add(sbMap.get("WORK_PROVER_ID").toString());
    	map.put("friendship", relativesAndFriendsDao.queryListInId(idList));
        return map;
    }
}